-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Suffix Array and LCP Array Implementation in Python #12166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This code file provides an implementation of Suffix Arrays and Longest Common Prefix (LCP) Arrays in Python, designed as a contribution to the open-source community during Hacktoberfest 2024. Overview: A suffix array is an essential data structure used in many string-processing algorithms. It provides an efficient way to store and sort all possible suffixes of a given string. This project also includes the construction of the LCP array, which records the lengths of the longest common prefixes between consecutive suffixes in the sorted suffix array. Together, these two arrays form the backbone of many algorithms in text processing and pattern matching. Key Features: Suffix Array Construction: A suffix array is built by sorting all suffixes of the input string in lexicographical order and storing their starting indices. LCP Array Construction: The LCP array is computed using an efficient algorithm that compares consecutive suffixes from the suffix array and records the length of their common prefixes. Optimized Approach: The approach used in this implementation ensures efficient computation of both suffix and LCP arrays with a linear-time construction of the LCP array following the suffix sorting. User-friendly Display: The program clearly displays both the suffix and LCP arrays, allowing users to easily visualize and understand the results for any given input string. Why this Contribution? As part of Hacktoberfest 2024, I wanted to contribute something that could be useful for developers and researchers working with text-processing algorithms. This implementation not only helps in better understanding of basic string operations but also serves as a building block for more complex algorithms in fields like bioinformatics, data compression, and natural language processing. Example Output: For the input string "banana", the program generates the following arrays: Suffix Array: [5, 3, 1, 0, 4, 2] (indicating the starting indices of the lexicographically sorted suffixes) LCP Array: [0, 1, 3, 0, 0, 2] (showing the lengths of the longest common prefixes between consecutive suffixes) Why Suffix Arrays and LCP Arrays Matter: Text Searching: Suffix arrays are used in algorithms for fast substring searching, making them invaluable in tasks like searching through large databases or text files. Repetitive Patterns: The LCP array highlights repeated patterns within the text, which can be useful in applications like data compression, where redundancy needs to be minimized. Bioinformatics: These arrays are critical for genome sequencing and alignment algorithms, where comparing large sequences efficiently is necessary. How to Use: This implementation is easy to run with any input string, and users can quickly get a clear visualization of the suffix and LCP arrays. Whether you're new to algorithms or looking to expand your toolkit for more advanced string manipulation tasks, this project provides a solid foundation.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
#!/usr/bin/env python3 | ||
|
||
|
||
def build_suffix_array(s: str) -> list[int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file divide_and_conquer/suffix_array_lcp.py
, please provide doctest for the function build_suffix_array
Please provide descriptive name for the parameter: s
return suffix_array | ||
|
||
|
||
def build_lcp_array(s: str, suffix_array: list[int]) -> list[int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file divide_and_conquer/suffix_array_lcp.py
, please provide doctest for the function build_lcp_array
Please provide descriptive name for the parameter: s
This pull request provides an implementation of Suffix Arrays and Longest Common Prefix (LCP) Arrays in Python, contributing to the open-source community as part of Hacktoberfest 2024.
Overview:
A suffix array is a fundamental data structure used in various text processing algorithms. This project implements:
Key Features:
Why this Contribution?
As part of Hacktoberfest 2024, this contribution aims to assist developers working with text-processing algorithms. This implementation serves as a foundation for more advanced algorithms in fields such as bioinformatics, data compression, and natural language processing.
Example Output:
For the input string
"banana"
, the program generates:[5, 3, 1, 0, 4, 2]
[0, 1, 3, 0, 0, 2]
Why Suffix Arrays and LCP Arrays Matter:
References:
For more information on suffix arrays and LCP arrays:
Describe your change:
Checklist: